package com.myPractice.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Spec;
import com.myPractice.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

@RestController
public class SpecController {
    @Autowired
    private SpecService specService;

    @GetMapping("/spec")
    public Result<List<Spec>> findAll() {
        return specService.findAll();
    }

    @PostMapping("/spec")
    public Result add(@RequestBody Spec spec) {
        boolean save = specService.save(spec);
        return new Result(save, null);
    }

    @GetMapping("/spec/search/{page}/{size}")
    public Result<List<Spec>> findPageByGet(@PathVariable("page") Long page, @PathVariable("size") Long size) {
        IPage<Spec> specPage = new Page<>(page, size);
        specService.page(specPage);
        List<Spec> list = specPage.getRecords();
        return new Result<List<Spec>>(true, null, list);
    }

    @PostMapping("/spec/search/{page}/{size}")
    public Result<List<Spec>> findPageByPost(@PathVariable("page") Long page, @PathVariable("size") Long size) {
        IPage<Spec> specPage = new Page<>(page, size);
        specService.page(specPage);
        List<Spec> list = specPage.getRecords();
        return new Result<List<Spec>>(true, null, list);
    }

    @GetMapping("/spec/{id}")
    public Result<Spec> findById(@PathVariable("id") Integer id){
        Spec spec = specService.getById(id);
        return new Result<Spec>(true,null,spec);
    }
    @PutMapping("/spec/{id}")
    public Result updateSpec(@RequestBody Spec spec,@PathVariable("id") Integer id){
        spec.setId(id);
        return specService.updateSpec(spec);
    }
    @DeleteMapping("/spec/{id}")
    public Result deleteBySpecId(@PathVariable("id") Integer id){
        boolean removeById = specService.removeById(id);
        return new Result(removeById,null);
    }
}
